package com.yix.study.tec.entity;

import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

/**
 * 饿汉式单例模式
 */
public class SingletonEager {
    public static void main(String[] args) throws IOException {
        System.out.println(System.getProperty("user.dir")+"\\src\\main\\resources\\static\\Bajie.jpg");
        JFrame jFrame = new JFrame("饿汉式单例模式测试");
        jFrame.setLayout(new GridLayout(1,2));
        Container container = jFrame.getContentPane();
        Bajie bajie = Bajie.getInstance();
        container.add(bajie);
        Bajie bajie1 = Bajie.getInstance();
        container.add(bajie);
        if(bajie == bajie1){
            System.out.println("他们是同一个人");
        }else {
            System.out.println("他们是同一个人");
        }
        jFrame.pack();
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

class Bajie extends JPanel{
    private static Bajie instance = new Bajie();

    private Bajie(){
        JLabel li = new JLabel(new ImageIcon(System.getProperty("user.dir")+"\\src\\main\\resources\\static\\Bajie.jpg"));
        this.add(li);
    }

    public static Bajie getInstance(){
        return instance;
    }

}